package com.erik.lin.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Erik on 2017/8/30.
 */
@Component
@Aspect
public class MapResultHandler {

    @Pointcut("@annotation(com.erik.lin.annotion.MapResult)")
    public void pointcut() {

    }

    @Around("pointcut()")
    public Map<String, Object> resultHandler(ProceedingJoinPoint joinPoint) throws Throwable {
        Map<String, Object> rs = null;
        try {
            rs = (Map<String, Object>) joinPoint.proceed();
            rs.put("success", true);
        }catch (Exception e) {
            rs = new HashMap();
            rs.put("success", false);
            rs.put("errMsg", e.getMessage());
            e.printStackTrace();
        }finally {
            return rs;
        }
    }
}
